/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.comfacauca.xmlc2.servlet;

import com.comfacauca.xmlc2.dao.ProyectosDAO;
import com.comfacauca.xmlc2.entidades.RequerimientosVw;
import com.comfacauca.xmlc2.servicio.ProyectosI;
import com.comfacauca.xmlc2.utils.Utiles;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author eorozco
 */
public class OcupacionServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    ProyectosI proyectosI = new ProyectosDAO();
    private List lista;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String f1 = request.getParameter("f1");
        String f2 = request.getParameter("f2");
        int usuario = Integer.parseInt(request.getParameter("usuario"));
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");       
        Date startDate = null;
        Date startDate1;
        Date endDate = null;
        
        try {
            startDate = sdf.parse(f1);
            endDate = sdf.parse(f2);
        } catch (ParseException ex) {
            Logger.getLogger(OcupacionServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        

        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);


        String[] fechas = new String[Utiles.calcularDiasRangoFechas(f1, f2)];
        
        int i=0;               
        for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
            fechas[i]=sdf.format(date);
            i++;
        }          
        
        lista = proyectosI.getItemsUsuario(usuario);        
                //Iterator con = this.lista.iterator();
                                
        String salida = "";

        salida+="<br /><table border='1' style='font-size:9;word-wrap: break-word;' ><tr>";

        String espacio = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"                     
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"
                        + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                
                
        /*CREA LOS ENCABEZADOS*/
        salida+="<tr style='background-color:#A9E2F3; font-weight:bold;font-size:10px'>"; 
        salida+="<td width='150px'>Responsable&nbsp&nbsp&nbsp&nbsp</td><td>Iteraci&oacute;n</td><td>Issue</td><td width='200px'>Descripci&oacute;n&nbsp;Item"+espacio+"</td><td>Estado</td>";
        for(int j=0; j<fechas.length;j++){
                    
                    if(fechas[j]!=null){
                        
                        Calendar cal = Calendar.getInstance();                        
                        try {                        
                            cal.setTime(sdf.parse(fechas[j]));
                        } catch (ParseException ex) {
                            Logger.getLogger(OcupacionServlet.class.getName()).log(Level.SEVERE, null, ex);
                        }
                        String color= "#A9E2F3";                        
                        if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
                            color="orange";
                        }
                        
                        String[] diames = fechas[j].split("-");
                        salida+="<td style='background-color:"+color+"'>"+diames[1]+"/"+diames[2]+"</td>";
                    }
                }
                salida+="</tr>";
        
        for (RequerimientosVw m : (List<RequerimientosVw>) lista) {
                   
                    salida+="<tr style='font-size:10px'>";
                        String color="#E6E6E6";
                        
                        
                        String desc = m.getTitulo();//(b[6].toString().length()>60)?b[6].toString().substring(0, 60):b[6].toString();
                        
                        
                        salida+="<td style='background-color:#D8D8D8'>"+m.getDescripcionResponsable()+
                                "</td><td style='background-color:#D8D8D8'>"+m.getCodigoIteracion()+
                                "</td><td style='background-color:#D8D8D8'>"+m.getCodigo()+
                                "</td><td style='background-color:#D8D8D8'>"+desc+                                
                                "</td><td style='background-color:#D8D8D7'>"+m.getDescripcionEstado()+"</td>";
                        
                        for(int k=0; k<fechas.length;k++){
                         
                             if(fechas[k]!=null){
                                 
                                 
                                 
                                    Calendar cal = Calendar.getInstance();                        
                                 try {
                                     cal.setTime(sdf.parse(fechas[k]));
                                     
//                                    if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
//                                        color="orange";
//                                    }
//                                 
                                 } catch (ParseException ex) {
                                     Logger.getLogger(OcupacionServlet.class.getName()).log(Level.SEVERE, null, ex);
                                 }
                                                                                                  
                                 
                                 if(m.getInicioRequerimiento().toString().equals(fechas[k])){
                                     color = "#F5A9E1";
                                 }
                                 
//                                  if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY){
//                                        color="orange";
//                                    }else{
//                                      
//                                  }
                                 
                                salida+="<td style='background-color:"+color+"'>";    
                                    salida+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
                                salida+="</td>";
                                
                                 if(m.getFinRequerimiento().toString().equals(fechas[k])){
                                     color = "#E6E6E6";
                                 }
                             }
                        }           
                        
                    salida+="</tr>";
                }
        
        salida+="</table><br />";               
        out.println(salida);
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
